package cn.zldemo;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import cn.zldemo.util.SetOnClickUtil;
import cn.zldemo.util.ViewMapping;
import cn.zldemo.util.ViewMappingUtil;

@ViewMapping(id = R.layout.activity_button)
public class ButtonActivity extends AppCompatActivity implements View.OnClickListener {
	@ViewMapping(id = R.id.btn1)
	Button btn1;
	@ViewMapping(id = R.id.btn2)
	Button btn2;
	@ViewMapping(id = R.id.btn3)
	Button btn3;
	@ViewMapping(id = R.id.btn4)
	Button btn4;
	@ViewMapping(id = R.id.btn5)
	Button btn5;

	Dialog dialog1;
	Dialog dialog2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_button);
		ViewMappingUtil.mapView(this, this);
		SetOnClickUtil.registerOnClick(this);
		initDialog1();
		initDialog2();
	}

	private void initDialog1() {
		dialog1 = new Dialog(this);
	}

	private void initDialog2() {
		dialog2 = new Dialog(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn4:
			dialog1.setTitle("正常的背景");
			WindowManager.LayoutParams lp = dialog1.getWindow().getAttributes();
			dialog1.getWindow().setAttributes(lp);
			dialog1.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
			dialog1.show();
			break;
		case R.id.btn5:
			dialog2.setTitle("dim 0.5");
			WindowManager.LayoutParams lp1 = dialog2.getWindow().getAttributes();
			lp1.dimAmount = 0.5f;
			dialog2.getWindow().setAttributes(lp1);
			dialog2.getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND,
					WindowManager.LayoutParams.FLAG_DIM_BEHIND);
			dialog2.show();
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_button, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}

		return super.onOptionsItemSelected(item);
	}
}
